<?php

namespace App\Http\Controllers\Laifei\Settlement;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ProxyStoreController extends Controller
{
    /**
     * 代理列表
     */
    public function index()
    {
        $all = $this->all;
        $pageSize = $all['pageSize'] ?? 20;
        $pageNo = $all['pageNo'] ?? 1;

        $where = function ($query) use ($all) {
            if (isset($all['we_id'])) {
                $query->where('lu.we_id', '=', $all['we_id']);
            }
            if (isset($all['openid'])) {
                $query->where('lu.openid', '=', $all['openid']);
            }
            if (isset($all['nickname'])) {
                $query->where('lu.nickname', 'like', '%'.$all['nickname'].'%');
            }
        };

        $total = DB::table('proxy_users as pu')
            ->leftJoin('lottery_users as lu','pu.we_id','=','lu.we_id')
            ->leftJoin('lottery_users as lu2','pu.we_id','=','lu2.proxy')
            ->where($where)
            ->groupBy('pu.we_id')
            ->count();

        $info = DB::table('proxy_users as pu')
            ->leftJoin('lottery_users as lu','pu.we_id','=','lu.we_id')
            ->leftJoin('lottery_users as lu2','pu.we_id','=','lu2.proxy')
            ->leftJoin('proxy_log as pl','pu.we_id','=','pl.proxy_we_id')
            ->where($where)
            ->select('lu.we_id','lu.nickname','lu.openid','pu.proxy_id','pu.proxy_code','pu.wallet','pu.created_at')
            ->selectRaw('COUNT(DISTINCT `lu2`.`we_id`) as user_count')
            ->groupBy('pu.proxy_id')
            ->skip(($pageNo - 1) * $pageSize)
            ->take($pageSize)
            ->get()
            ->map(function ($item) {
                $item->wallet = $item->wallet / 100.00;
                return $item;
            });

        $we_ids = $info->pluck('we_id');
        $info = $info->toArray();
        $info = json_decode(json_encode($info), true);

        $price = DB::table('proxy_log')
            ->whereIn('proxy_we_id',$we_ids)
            ->select('proxy_we_id')
            ->selectRaw('SUM(CASE WHEN type <> 0 THEN proxy_price ELSE 0 END) as total')
            ->selectRaw('SUM(CASE WHEN type = 0 THEN proxy_price ELSE 0 END) as total_withdraw')
            ->groupBy('proxy_we_id')
            ->get()
            ->mapWithKeys(function($item){
                return [$item->proxy_we_id => [
                    'total' => $item->total / 100.00,
                    'total_withdraw' => $item->total_withdraw / 100.00,
                ]];
            })
            ->toArray();
        $price = json_decode(json_encode($price), true);

        foreach($info as &$value){
            if(isset($price[$value['we_id']])){
                $value['total'] = $price[$value['we_id']]['total'];
                $value['total_withdraw'] = $price[$value['we_id']]['total_withdraw'];
            }
        }

        return $this->setReturnJson(0, 'success', ['total' => $total, 'info' => $info]);

    }

}
